package org.zero.concurrency.demo.example.unsafeclass;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @program: concurrency-demo
 * @description:
 * @author: 01
 * @create: 2018-10-21 16:42
 **/
public class HashMapExample2 {

    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();
        map.put("a", 1);
        map.put("b", 2);

        Iterator<Map.Entry<String, Integer>> entryIterator = map.entrySet().iterator();
        while (entryIterator.hasNext()) {
            Map.Entry<String, Integer> next = entryIterator.next();
            System.out.println("key=" + next.getKey() + " value=" + next.getValue());
        }

        Iterator<String> iterator = map.keySet().iterator();
        while (iterator.hasNext()) {
            String key = iterator.next();
            System.out.println("key=" + key + " value=" + map.get(key));
        }

        // 等同于第一种方式
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            System.out.println("key=" + entry.getKey() + " value=" + entry.getValue());
        }
    }
}
